package cn.skyui.animator;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import cn.skyui.animator.feeds.FeedsActivity;
import cn.skyui.animator.photo.SinglePhotoActivity;

public class MainActivity extends AppCompatActivity {

    boolean scale = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.imageView);
        int screenWidth = getResources().getDisplayMetrics().widthPixels;

        imageView.setOnClickListener(v -> {
            if(!scale) {
                ObjectAnimator moveX = ObjectAnimator.ofFloat(imageView, "translationX", 0f, (screenWidth - imageView.getWidth()) / 2);
                ObjectAnimator moveY = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 500);

                ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 2);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 2);

                ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);

                ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);

                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(300);
                animatorSet.playTogether(moveX, moveY, scaleX, scaleY, alpha, rotate);
                animatorSet.start();
            } else {
                ObjectAnimator moveX = ObjectAnimator.ofFloat(imageView, "translationX", imageView.getTranslationX(), 0);
                ObjectAnimator moveY = ObjectAnimator.ofFloat(imageView, "translationY", imageView.getTranslationY(), 0);

                ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1);

                ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);

                ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);

                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(300);
                animatorSet.playTogether(moveX, moveY, scaleX, scaleY, alpha, rotate);
                animatorSet.start();
            }
            scale = !scale;
        });



        ImageView shareImageView = findViewById(R.id.shareImageView);
        shareImageView.setOnClickListener(v -> {
            Intent intent = new Intent(this, SinglePhotoActivity.class);
            // Pass data object in the bundle and populate details activity.
            // intent.putExtra(SinglePhotoActivity.EXTRA_CONTACT, contact);
            ActivityOptionsCompat options = ActivityOptionsCompat.
                    makeSceneTransitionAnimation(this, (View)shareImageView, "image");
            startActivity(intent, options.toBundle());
        });


        findViewById(R.id.feeds).setOnClickListener(v -> {
            Intent intent = new Intent(this, FeedsActivity.class);
            startActivity(intent);
        });
    }

}
